﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing;

namespace Hue {
	class Util {

		public static Rectangle Rect(RectangleF rf) {
			Rectangle r = new Rectangle();
			r.X = (int)rf.X;
			r.Y = (int)rf.Y;
			r.Width = (int)rf.Width;
			r.Height = (int)rf.Height;
			return r;
		}

		public static RectangleF Rect(Rectangle r) {
			RectangleF rf = new RectangleF();
			rf.X = (float)r.X;
			rf.Y = (float)r.Y;
			rf.Width = (float)r.Width;
			rf.Height = (float)r.Height;
			return rf;
		}

		public static Point Point(PointF pf) {
			return new Point((int)pf.X, (int)pf.Y);
		}

		public static PointF Center(RectangleF r) {
			PointF center = r.Location;
			center.X += r.Width / 2;
			center.Y += r.Height / 2;
			return center;
		}
		
		public static void DrawFrame(Graphics dc, RectangleF r, float cornerRadius, Color color) {
			Pen pen = new Pen(color);
			if (cornerRadius <= 0) {
				dc.DrawRectangle(pen, Rect(r));
				return;
			}
			cornerRadius = (float)Math.Min(cornerRadius, Math.Floor(r.Width) - 2);
			cornerRadius = (float)Math.Min(cornerRadius, Math.Floor(r.Height) - 2);

			GraphicsPath path = new GraphicsPath();
			path.AddArc(r.X, r.Y, cornerRadius, cornerRadius, 180, 90);
			path.AddArc(r.Right - cornerRadius, r.Y, cornerRadius, cornerRadius, 270, 90);
			path.AddArc(r.Right - cornerRadius, r.Bottom - cornerRadius, cornerRadius, cornerRadius, 0, 90);
			path.AddArc(r.X, r.Bottom - cornerRadius, cornerRadius, cornerRadius, 90, 90);
			path.CloseAllFigures();
			dc.DrawPath(pen, path);
		}
		
		public static void Draw2ColorBar(Graphics dc, RectangleF r, Orientation orientation, Color c1, Color c2) {
			RectangleF lr1 = r;
			float angle = 0;

			if (orientation == Orientation.Vertical)
				angle = 270;
			if (orientation == Orientation.Horizontal)
				angle = 0;

			if (lr1.Height > 0 && lr1.Width > 0) {
				LinearGradientBrush lb1 = new LinearGradientBrush(lr1, c1, c2, angle, false);
				dc.FillRectangle(lb1, lr1);
				lb1.Dispose();
			}
		}
		
		public static void Draw3ColorBar(Graphics dc, RectangleF r, Orientation orientation, Color c1, Color c2, Color c3) {
			// to draw a 3 color bar 2 gradient brushes are needed
			// one from c1 - c2 and c2 - c3
			RectangleF lr1 = r;
			RectangleF lr2 = r;
			float angle = 0;

			if (orientation == Orientation.Vertical) {
				angle = 270;

				lr1.Height = lr1.Height / 2;
				lr2.Height = r.Height - lr1.Height;
				lr2.Y += lr1.Height;
			}
			if (orientation == Orientation.Horizontal) {
				angle = 0;

				lr1.Width = lr1.Width / 2;
				lr2.Width = r.Width - lr1.Width;
				lr1.X = lr2.Right;
			}

			if (lr1.Height > 0 && lr1.Width > 0) {
				LinearGradientBrush lb2 = new LinearGradientBrush(lr2, c1, c2, angle, false);
				LinearGradientBrush lb1 = new LinearGradientBrush(lr1, c2, c3, angle, false);

				dc.FillRectangle(lb1, lr1);
				dc.FillRectangle(lb2, lr2);

				lb1.Dispose();
				lb2.Dispose();
			}
			// with some sizes the first pixel in the gradient rectangle shows the opposite color
			// this is a workaround for that problem
			if (orientation == Orientation.Vertical) {
				Pen pc2 = new Pen(c2, 1);
				Pen pc3 = new Pen(c3, 1);
				dc.DrawLine(pc3, lr1.Left, lr1.Top, lr1.Right - 1, lr1.Top);
				dc.DrawLine(pc2, lr2.Left, lr2.Top, lr2.Right - 1, lr2.Top);
				pc2.Dispose();
				pc3.Dispose();
			}

			if (orientation == Orientation.Horizontal) {
				Pen pc1 = new Pen(c1, 1);
				Pen pc2 = new Pen(c2, 1);
				Pen pc3 = new Pen(c3, 1);
				dc.DrawLine(pc1, lr2.Left, lr2.Top, lr2.Left, lr2.Bottom - 1);
				dc.DrawLine(pc2, lr2.Right, lr2.Top, lr2.Right, lr2.Bottom - 1);
				dc.DrawLine(pc3, lr1.Right, lr1.Top, lr1.Right, lr1.Bottom - 1);
				pc1.Dispose();
				pc2.Dispose();
				pc3.Dispose();
			}
		}
	}
}
